package gw.com.android.ui.trade;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;

import gw.com.android.app.AppMain;
import gw.com.android.model.DataManager;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.ui.quote2.QuoteChartConfig;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.DoubleConverter;
import www.com.library.util.JsonUtil;

/**
 * 不同产品的下单说明的帮助类
 *
 * @author jett
 * @since 2018-04-20.
 */
class OrderExplainHelper {

    /**
     * 手数帮助
     */
    static View buildLotHelpView(Context context, int code, String name) {
        View view = View.inflate(context, R.layout.view_explain_order_lot, null);
        TextView tvName = view.findViewById(R.id.explain_product_name);
        TextView tvLot = view.findViewById(R.id.explain_lot);
        TextView tvCurrency = view.findViewById(R.id.explain_currency);
        TextView tvQuantity = view.findViewById(R.id.explain_quantity);

        tvName.setText(name);

        String jsonStr = AppTerminal.instance().getProtertyModel(code);
        DataItemDetail item = new DataItemDetail();
        try {
            JSONObject mArray = new JSONObject(jsonStr);
            JsonUtil.toDataItemDetail(item, mArray);

            String lotUnit = item.getString(GTSConst.JSON_KEY_CONTRACTSIZE);
            boolean isCNY = DataManager.instance().isCNY();
            String minLot = item.getString(GTSConst.JSON_KEY_VOLUMESMIN);
            String maxLot = item.getString(GTSConst.JSON_KEY_VOLUMESMAX);
            String lotQuantity = minLot.concat("-").concat(maxLot);

            String lot = context.getResources().getString(R.string.order_explain_unit_name);
            lot = String.format(Locale.getDefault(), lot, lotUnit);
            tvLot.setText(lot);

            tvCurrency.setText(isCNY ? R.string.CNH : R.string.USD);

            String quantity = context.getResources().getString(R.string.order_explain_hand);
            quantity = String.format(Locale.getDefault(), quantity, lotQuantity);
            tvQuantity.setText(quantity);

            return view;
        } catch (JSONException e) {
            Logger.e(e);
        }
        return null;
    }

    /**
     * 保证金
     */
    static View buildBailHelpView(Context context, int code, String name) {
        View view = View.inflate(context, R.layout.view_explain_bail, null);
        TextView tvName = view.findViewById(R.id.explain_product_name);
        TextView tvInitBail = view.findViewById(R.id.explain_init_bail);
        TextView tvVacationBail = view.findViewById(R.id.explain_vacation_bail);
        TextView tvLockBail = view.findViewById(R.id.explain_lock_bail);

        tvName.setText(name);

        DataItemDetail item;
        if (DataManager.instance().hasMargin()) {
            item = getMarginBail(code);
        } else {
            item = getUnMarginBail(code);
        }
        if (item == null) {
            return null;
        }
        String init = item.getString(GTSConst.JSON_KEY_MARGININITIAL);
        String vacation = item.getString(GTSConst.JSON_KEY_HOLIDAYMARGININITIAL);
        String lock = item.getString(GTSConst.JSON_KEY_MARGINHEDGED);

        String initBail = context.getResources().getString(R.string.order_explain_bail_amount);
        initBail = String.format(Locale.getDefault(), initBail, init);
        tvInitBail.setText(initBail);

        String vacationBail = context.getResources().getString(R.string.order_explain_bail_amount);
        vacationBail = String.format(Locale.getDefault(), vacationBail, vacation);
        tvVacationBail.setText(vacationBail);

        String lockBail = context.getResources().getString(R.string.order_explain_bail_percent);
        lockBail = String.format(Locale.getDefault(), lockBail, lock) + "%";
        tvLockBail.setText(lockBail);

        return view;
    }

    private static DataItemDetail getUnMarginBail(int code) {
        String jsonStr = AppTerminal.instance().getProtertyModel(code);
        DataItemDetail item = new DataItemDetail();
        try {
            JSONObject mArray = new JSONObject(jsonStr);
            JsonUtil.toDataItemDetail(item, mArray);
        } catch (JSONException e) {
            Logger.e(e);
        }
        return item;
    }

    private static DataItemDetail getMarginBail(int code) {
        String jsonStr2 = AppTerminal.instance().getMarginLevel(code);
        DataItemResult mMarginList = new DataItemResult();
        DataItemDetail result = new DataItemDetail();
        try {
            JSONArray mArray2 = new JSONArray(jsonStr2);
            JsonUtil.toDataItemResult(mMarginList, mArray2);
        } catch (JSONException e) {
            Logger.e(e);
        }

        boolean isCNY = DataManager.instance().isCNY();
        if (mMarginList.getDataCount() > 0) {
            DataItemDetail first = mMarginList.getItem(0);
            result.setStringValue(GTSConst.JSON_KEY_MARGININITIAL, calCNY(isCNY, first.getString(GTSConst.JSON_KEY_MARGININITIAL)));
            result.setStringValue(GTSConst.JSON_KEY_HOLIDAYMARGININITIAL, calCNY(isCNY, first.getString(GTSConst.JSON_KEY_HOLIDAYMARGININITIAL)));
            result.setStringValue(GTSConst.JSON_KEY_MARGINHEDGED, first.getString(GTSConst.JSON_KEY_MARGINHEDGED));
            return result;
        }
        return null;
    }

    private static String calCNY(boolean isCNY, String value) {
        if (!isCNY) {
            return DoubleConverter.toStringData(DoubleConverter.toDoubleData(value), 2);
        }
        double rate = 0;
        DataItemDetail item = DataManager.instance().getTickModel(GTSConst.CNY_CODE);
        if (null != item) {
            rate = DoubleConverter.toDoubleData(item.getString(GTSConst.JSON_KEY_CURPRICE));
        }
        double result = DoubleConverter.toDoubleData(value) * rate;
        return DoubleConverter.toStringData(result, 2);
    }

    /**
     * 合约单位
     */
    private static String getLotUnit(int code) {
        switch (code) {
            case QuoteChartConfig.LONDON_GOLD:
                return "100";
            case QuoteChartConfig.LONDON_SILVER:
                return "5000";
            case QuoteChartConfig.LIAN_GOLD:
                return "1000";
            case QuoteChartConfig.LIAN_SILVER:
                return "30";
            default:
                return "";
        }
    }

    /**
     * 货币单位
     */
    private static String getLotCurrency(int code) {
        return AppMain.getApp().getString(R.string.order_explain_usd);
    }

    /**
     * 单笔交易手数
     */
    private static String getLotQuantity(int code) {
        switch (code) {
            case QuoteChartConfig.LONDON_GOLD:
                return "0.01-30";
            case QuoteChartConfig.LONDON_SILVER:
                return "0.01-30";
            case QuoteChartConfig.LIAN_GOLD:
                return "0.1-100";
            case QuoteChartConfig.LIAN_SILVER:
                return "0.1-50";
            default:
                return "";
        }
    }

    /**
     * 初始保证金
     */
    private static String getInitBail(int code) {
        switch (code) {
            case QuoteChartConfig.LONDON_GOLD:
                return "500";
            case QuoteChartConfig.LONDON_SILVER:
                return "500";
            case QuoteChartConfig.LIAN_GOLD:
                return "400";
            case QuoteChartConfig.LIAN_SILVER:
                return "200";
            default:
                return "200";//这里的默认值要改成和IOS一样
        }
    }

    /**
     * 假期保证金
     */
    private static String getVacationBail(int code) {
        switch (code) {
            case QuoteChartConfig.LONDON_GOLD:
                return "500";
            case QuoteChartConfig.LONDON_SILVER:
                return "500";
            case QuoteChartConfig.LIAN_GOLD:
                return "400";
            case QuoteChartConfig.LIAN_SILVER:
                return "200";
            default:
                return "400";
        }
    }

    /**
     * 锁仓保证金
     */
    private static String getLockBail(int code) {
        switch (code) {
            case QuoteChartConfig.LONDON_GOLD:
                return "50";
            case QuoteChartConfig.LONDON_SILVER:
                return "50";
            case QuoteChartConfig.LIAN_GOLD:
                return "50";
            case QuoteChartConfig.LIAN_SILVER:
                return "50";
            default:
                return "25";
        }
    }

}
